#!/usr/bin/env sh
source ~/.profile 
####################################################################
#incude tools script
tool_name=$HOME/git/tool_sh/shell_tool.sh
if [ -f $tool_name ] ;then
    source $tool_name 
else
    echo "-- errot,file not exist,which name is:$tool_name."
    exit 1
fi
tm_data_file=$HOME/git/tool_sh/tm_data.sh
if [  -f $tm_data_file ]; then
    source $tm_data_file  
else
    echo -- error, $tm_data_file file not exist,please check it!
    exit 1
fi

#$1:project dir
build_project()
{
    local ret=0
    local cur_dir_build_project=`pwd`
    local project_dir_tmp=$1
    cd $project_dir_tmp
    project_dir_tmp=`pwd`
    build_project_project_name=`basename $project_dir_tmp`

    echo "-- go to build -----$project_dir_tmp"
    if [ ! -d build ] ;then
        mkdir -p build
    fi

    echo "-- cmake .. && make"
    cd build && cmake .. && make
    ret=$?
    if [ $ret -ne 0 ]; then
        echo -e "-- project $build_project_project_name complie failed!\n"
        exit 201
    else
        echo -e "-- project $build_project_project_name complie succeed.\n"
    fi

    cd $cur_dir_build_project
    return $ret
}

#$1:project dir
#$2:cmake flags
build_lib_so()
{
    local ret=0
    local cur_dir_build_project=`pwd`
    local project_dir_tmp=$1
    cmake_flags_tmp=$2
    cd $project_dir_tmp
    project_dir_tmp=`pwd`
    build_project_project_name=`basename $project_dir_tmp`

    echo "-- go to build -----$project_dir_tmp"
    if [ ! -d build ] ;then
        mkdir -p build
    fi

    echo "-- cmake .. -DBUILD_SHARED_LIBS=ON $cmake_flags_tmp && make"
    cd build && cmake .. -DBUILD_SHARED_LIBS=ON $cmake_flags_tmp&& make
    ret=$?
    if [ $ret -ne 0 ]; then
        echo -e "-- project $build_project_project_name complie failed!\n"
        exit 1
    else
        echo -e "-- project $build_project_project_name complie succeed.\n"
    fi

    cd $cur_dir_build_project
    return $ret
}

#1:test dir
build_test()
{
    cur_dir_build_test_tmp=`pwd`
    project_dir_tmp=$1
    if [ ! -d $project_dir_tmp ]; then
        echo "-- directory $project_dir_tmp is not exist."
        return
    fi 
    cd $project_dir_tmp
    project_dir_tmp=`pwd`
    #/..../cfg/test
    build_test_project_name=`dirname $project_dir_tmp`
    build_test_project_name=`basename $build_test_project_name`

    cd $project_dir_tmp/build

    build_project $project_dir_tmp

    test_file=test_$build_test_project_name
    if [ -f  $test_file ]; then
        echo "-- run $test_file"
        ./$test_file
    else
        echo "-- warning,$test_file does not exist!"
    fi

    cd $cur_dir_build_test_tmp
    unset cur_dir_build_test_tmp
}

set_zk_connection_env()
{
    env | grep zookeeper_connection_string > /dev/null
    ##if not found it int env
    #if [ $? -eq 1 ];then 
    #    insert_profile "zookeeper_connection_string" "10.45.4.240:2181" 1 
    #fi
}



